package com.ruoyi.eqmonitor.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.eqmonitor.domain.OptDeviceRelation;
import com.ruoyi.eqmonitor.dtos.OptReAreaDto;
import com.ruoyi.eqmonitor.mapper.OptDeviceRelationMapper;
import com.ruoyi.eqmonitor.service.IOptDeviceRelationService;
import com.ruoyi.eqmonitor.service.IPipeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 设备与其他事物的关联关系Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-16
 */
@Service
public class OptDeviceRelationServiceImpl extends ServiceImpl<OptDeviceRelationMapper, OptDeviceRelation> implements IOptDeviceRelationService {

    @Resource
    private IPipeService iPipeService;

    @Override
    public OptDeviceRelation selectOptDeviceRelationByDeviceCode(String deviceCode){
        return baseMapper.selectOptDeviceRelationByDeviceCode(deviceCode);
    }

    @Override
    public List<OptDeviceRelation> selectOptDeviceRelationList(OptDeviceRelation optDeviceRelation){
        return baseMapper.selectOptDeviceRelationList(optDeviceRelation);
    }

    @Override
    public List<OptReAreaDto> selectOptReArea() {
        return baseMapper.selectOptReArea();
    }

    @Override
    public OptDeviceRelation getByCode(String snCode) {
        return baseMapper.getByCode(snCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDeviceRelation(OptDeviceRelation optDeviceRelation) {
        Assert.notNull(optDeviceRelation.getAreaId(), "所属片区不允许为空");
        Assert.notBlank(optDeviceRelation.getDeviceCode(), "所属设备不允许为空");

        if (StrUtil.isNotBlank(optDeviceRelation.getPipeWtdh())) {
            Assert.notBlank(optDeviceRelation.getPipeType(), "请选择管线类型");
            Map<String, Object> pointInfo = iPipeService.getPointByWtdh(optDeviceRelation.getPipeType(), optDeviceRelation.getPipeWtdh());
            if (MapUtil.isEmpty(pointInfo)) throw new ServiceException(StrUtil.format("管点不存在: 物探点号{}, 管线类型: {}", optDeviceRelation.getPipeWtdh(), optDeviceRelation.getPipeType()));
            // 赋值井深
            optDeviceRelation.setPipeJs(MapUtil.getDouble(pointInfo, "JS"));

            // 赋值所有与之相连的管线
            List<Map<String, Object>> lineList = iPipeService.getLineByPointWtdh(optDeviceRelation.getPipeType(), optDeviceRelation.getPipeWtdh());
            if (CollectionUtil.isNotEmpty(lineList)) {
                Set<String> collect = lineList.stream().map(mp -> {
                    String qddh = MapUtil.getStr(mp, "QDDH");
                    String zddh = MapUtil.getStr(mp, "ZDDH");
                    return StrUtil.format("{}_{}", qddh, zddh);
                }).collect(Collectors.toSet());
                optDeviceRelation.setPipeLines(CollectionUtil.join(collect, ","));
            }
        }
        return baseMapper.updateById(optDeviceRelation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveDeviceRelation(OptDeviceRelation optDeviceRelation) {
        Assert.notNull(optDeviceRelation.getAreaId(), "所属片区不允许为空");
        Assert.notBlank(optDeviceRelation.getDeviceCode(), "所属设备不允许为空");

        if (StrUtil.isNotBlank(optDeviceRelation.getPipeWtdh())) {
            Assert.notBlank(optDeviceRelation.getPipeType(), "请选择管线类型");
            Map<String, Object> pointInfo = iPipeService.getPointByWtdh(optDeviceRelation.getPipeType(), optDeviceRelation.getPipeWtdh());
            if (MapUtil.isEmpty(pointInfo)) throw new ServiceException(StrUtil.format("管点不存在: 物探点号{}, 管线类型: {}", optDeviceRelation.getPipeWtdh(), optDeviceRelation.getPipeType()));
            // 赋值井深
            optDeviceRelation.setPipeJs(MapUtil.getDouble(pointInfo, "JS"));

            // 赋值所有与之相连的管线
            List<Map<String, Object>> lineList = iPipeService.getLineByPointWtdh(optDeviceRelation.getPipeType(), optDeviceRelation.getPipeWtdh());
            if (CollectionUtil.isNotEmpty(lineList)) {
                Set<String> collect = lineList.stream().map(mp -> {
                    String qddh = MapUtil.getStr(mp, "QDDH");
                    String zddh = MapUtil.getStr(mp, "ZDDH");
                    return StrUtil.format("{}_{}", qddh, zddh);
                }).collect(Collectors.toSet());
                optDeviceRelation.setPipeLines(CollectionUtil.join(collect, ","));
            }
        }
        return baseMapper.insert(optDeviceRelation);
    }

    @Override
    public int deleteByDeviceCode(String deviceCode) {
        return baseMapper.deleteById(deviceCode);
    }
}
